package com.zx.cloud.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zx.cloud.ApiVersion;
import com.zx.cloud.common.auth0.jwt.Token;
import com.zx.cloud.common.filter.CurrentUserId;
import com.zx.cloud.common.filter.IgnoreAuth;
import com.zx.cloud.common.filter.MagicConstants;
import com.zx.cloud.controller.client.ProviderClient;
import com.zx.cloud.model.request.ManagerLoginRequest;
import com.zx.cloud.model.response.ObjectDataResponse;
import com.zx.cloud.util.CookieManager;

@RestController
@RequestMapping(value = ApiVersion.MANAGER_V1_PREFIX + "/demo")
public class DemoController {

	private static final Logger logger = Logger.getLogger(DemoController.class);

	@Autowired
	private ProviderClient providerClient;

	@ApiOperation(value = "测试接口", notes = "测试接口")
	@RequestMapping(value = "/test05", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Long> test(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId) {
		System.out.println("---------------------test---------------");
		System.out.println("---------------------test-------------2--"+userId);
		return new ObjectDataResponse<Long>(userId);
	}
	
	@ApiOperation(value = "测试接口", notes = "测试接口")
	@RequestMapping(value = "/test", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<ManagerLoginRequest> test(@RequestBody ManagerLoginRequest loginRequest) {
		System.out.println("---------------------test---------------");
		ObjectDataResponse<ManagerLoginRequest> loginRequestObj = providerClient.demoTest(loginRequest);
		System.out.println("---------------------test-------------2--"+loginRequestObj);
		return loginRequestObj;
	}
	
	@ApiOperation(value = "测试接口", notes = "测试接口")
	@RequestMapping(value = "/test2", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<ManagerLoginRequest> test2(@RequestBody ManagerLoginRequest loginRequest) {
		ObjectDataResponse<ManagerLoginRequest> loginRequestObj = providerClient.demoTest2(loginRequest);
		return loginRequestObj;
	}
	
	@IgnoreAuth
	@ApiOperation(value = "测试接口", notes = "测试接口")
	@RequestMapping(value = "/test3", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<ManagerLoginRequest> test3() {
		ObjectDataResponse<ManagerLoginRequest> loginRequestObj = providerClient.demoTest3();
		return loginRequestObj;
	}
	
	@IgnoreAuth
	@ApiOperation(value = "测试接口", notes = "测试接口")
	@RequestMapping(value = "/loginTest", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Token> loginTest(HttpServletRequest request,HttpServletResponse response,@RequestBody ManagerLoginRequest loginRequest) {
		ObjectDataResponse<Token> loginRequestObj = providerClient.loginTest(loginRequest);
		new CookieManager(request, response).setCookie(MagicConstants.AUTH_TOKEN, loginRequestObj.getData().getToken(), 7*24*60*60);
		return loginRequestObj;
	}
	
}
